package com.zhaoc.mq;

import com.aliyuncs.exceptions.ClientException;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.Map;

/**
 * <p>title:com.zhaoc.bos.mq</p>
 * date:2018/8/11 0011
 * time:20:10
 * author:赵成
 * user:13566
 */
@Service("smsConsumer")
public class SmsConsumer implements MessageListener {

    @Override
    public void onMessage(Message message) {
        MapMessage mapMessage = (MapMessage) message;

        //调用sms服务发送短信
        try {
            String telephone = mapMessage.getString("telephone");

            Map<String, String> map = SmsUtils.sendSms(telephone);
            String resoult = "OK";
            if (resoult.equalsIgnoreCase(map.get("message"))){
                //将验证码存进session中
                ServletActionContext.getRequest().getSession().setAttribute(telephone, map.get("code"));
                System.out.println("短信发送成功");
            }else {
                System.out.println("发送失败,返回码是:"+map.get("message"));
            }
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (JMSException e) {
            e.printStackTrace();
        }

}
}
